//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.service.impl;

import ciicos.base.UserInfo;
import ciicos.base.entity.ModuleEntity;
import ciicos.base.entity.SuperBaseEntity;
import ciicos.base.model.module.ModuleModel;
import ciicos.base.service.ModuleService;
import ciicos.base.service.SuperServiceImpl;
import ciicos.engine.entity.FlowTemplateEntity;
import ciicos.engine.model.flowengine.FlowPagination;
import ciicos.engine.service.FlowTemplateJsonService;
import ciicos.engine.service.FlowTemplateService;
import ciicos.entity.AppDataEntity;
import ciicos.mapper.AppDataMapper;
import ciicos.model.AppDataListAllVO;
import ciicos.model.AppFlowListAllVO;
import ciicos.model.login.UserMenuModel;
import ciicos.permission.model.authorize.AuthorizeVO;
import ciicos.permission.service.AuthorizeService;
import ciicos.service.AppDataService;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import ciicos.util.treeutil.SumTree;
import ciicos.util.treeutil.newtreeutil.TreeDotUtils;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppDataServiceImpl extends SuperServiceImpl<AppDataMapper, AppDataEntity> implements AppDataService {
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private AuthorizeService authorizeService;
    @Autowired
    private FlowTemplateService flowTemplateService;
    @Autowired
    private FlowTemplateJsonService flowTemplateJsonService;

    public List<AppDataEntity> getList(String type) {
        UserInfo userInfo = this.userProvider.get();
        QueryWrapper<AppDataEntity> queryWrapper = new QueryWrapper<AppDataEntity>();
        queryWrapper.lambda().eq(AppDataEntity::getObjectType, type)
                .eq(AppDataEntity::getCreatorUserId, userInfo.getUserId());
        List<AppDataEntity> list = this.list(queryWrapper);
        list = (List)list.stream().filter((t) -> StringUtil.isNotEmpty(t.getSystemId()) && t.getSystemId().equals(userInfo.getSystemId())).collect(Collectors.toList());
        List<String> idAll = (List)list.stream().map(AppDataEntity::getObjectId).collect(Collectors.toList());
        List<FlowTemplateEntity> templateList = this.flowTemplateService.getTemplateList(idAll);

        for(int i = 0; i < list.size(); ++i) {
            AppDataEntity appDataEntity = (AppDataEntity)list.get(i);
            if ("2".equals(type)) {
                ModuleEntity info = this.moduleService.getInfo(appDataEntity.getObjectId());
                if (info == null || info.getEnabledMark() == 0) {
                    list.remove(i);
                    --i;
                }
            } else {
                FlowTemplateEntity templateEntity = (FlowTemplateEntity)
                        templateList.stream().filter((t) -> ((String)t.getId()).equals(appDataEntity.getObjectId())).findFirst().get();
                if (templateEntity == null) {
                    list.remove(i);
                    --i;
                }
            }
        }

        return list;
    }

    public List<AppDataEntity> getList() {
        QueryWrapper<AppDataEntity> queryWrapper = new QueryWrapper();
        return this.list(queryWrapper);
    }

    public AppDataEntity getInfo(String objectId) {
        UserInfo userInfo = this.userProvider.get();
        LambdaQueryWrapper<AppDataEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(AppDataEntity::getObjectId, objectId).eq(SuperBaseEntity.SuperCBaseEntity::getCreatorUserId, userInfo.getUserId());
        List<AppDataEntity> appDataEntities = this.list(queryWrapper);
        return ObjectUtil.isNotEmpty(appDataEntities) && appDataEntities.size() > 1 ? (AppDataEntity)appDataEntities.get(0) : (AppDataEntity)this.getOne(queryWrapper);
    }

    public boolean isExistByObjectId(String objectId, String systemId) {
        UserInfo userInfo = this.userProvider.get();
        QueryWrapper<AppDataEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(AppDataEntity::getObjectId, objectId)
                .eq(SuperBaseEntity.SuperCBaseEntity::getCreatorUserId, userInfo.getUserId()).eq(AppDataEntity::getSystemId, systemId);
        return this.count(queryWrapper) > 0L;
    }

    public void create(AppDataEntity entity) {
        UserInfo userInfo = this.userProvider.get();
        entity.setId(RandomUtil.uuId());
        entity.setCreatorUserId(userInfo.getUserId());
        entity.setCreatorTime(new Date());
        entity.setEnabledMark(1);
        entity.setSystemId(userInfo.getSystemId());
        this.save(entity);
    }

    public void delete(AppDataEntity entity) {
        this.removeById((Serializable)entity.getId());
    }

    public void delete(String objectId) {
        QueryWrapper<AppDataEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(AppDataEntity::getObjectId, objectId);
        this.remove(queryWrapper);
    }

    public List<AppFlowListAllVO> getFlowList(FlowPagination pagination) {
        List<String> objectId = (List)this.getList("1").stream().map(AppDataEntity::getObjectId).collect(Collectors.toList());
        List<FlowTemplateEntity> pageList = this.flowTemplateService.getListAll(pagination, true);
        List<AppFlowListAllVO> result = new ArrayList();

        for(FlowTemplateEntity entity : pageList) {
            AppFlowListAllVO vo = (AppFlowListAllVO)JsonUtil.getJsonToBean(entity, AppFlowListAllVO.class);
            vo.setIsData(objectId.contains(vo.getId()));
            result.add(vo);
        }

        return pagination.setData(result, pagination.getTotal());
    }

    public List<AppDataListAllVO> getDataList(String type) {
        List<AppDataEntity> dataList = this.getList(type);
        AuthorizeVO authorizeModel = this.authorizeService.getAuthorize(true);
        List<ModuleModel> buttonList = authorizeModel.getModuleList();
        List<ModuleEntity> menuList = (List)this.moduleService.getList().stream().filter((t) -> "App".equals(t.getCategory()) && t.getEnabledMark() == 1).collect(Collectors.toList());
        List<UserMenuModel> list = new LinkedList();

        for(ModuleEntity module : menuList) {
            boolean count = buttonList.stream().filter((t) -> t.getId().equals(module.getId())).count() > 0L;
            UserMenuModel userMenuModel = (UserMenuModel)JsonUtil.getJsonToBean(module, UserMenuModel.class);
            if (count) {
                boolean isData = dataList.stream().filter((t) -> t.getObjectId().equals(module.getId())).count() > 0L;
                userMenuModel.setIsData(isData);
                list.add(userMenuModel);
            }
        }

        List<SumTree<UserMenuModel>> menuAll = TreeDotUtils.convertListToTreeDot(list);
        List<AppDataListAllVO> menuListAll = JsonUtil.getJsonToList(menuAll, AppDataListAllVO.class);
        List<AppDataListAllVO> data = new LinkedList();

        for(AppDataListAllVO appMenu : menuListAll) {
            if ("-1".equals(appMenu.getParentId())) {
                data.add(appMenu);
            }
        }

        return data;
    }
}
